/*
A Pandemic Crossing the Border: The Impact of Covid-19 in the US on the Mexican Labor Market
Rojas and Yu, 2023

Replication file for Figure 3
*/

clear all
set more off

global data "COMPLETE YOUR DIRECTORY HERE \data"
global output "COMPLETE YOUR DIRECTORY HERE \output"

use "$data/cleaned_ind_daily.dta", clear

destring monthincomeifwork whrsifwork hrwageifwork, replace force

gen t=.
replace t=1 if qtr==12019
replace t=2 if qtr==22019
replace t=3 if qtr==32019
replace t=4 if qtr==42019
replace t=5 if qtr==12020
replace t=6 if qtr==32020
replace t=7 if qtr==42020

label define quarter_lab 1 "Q1-2019" 2 "Q2-2019" 3 "Q3-2019" 4 "Q4-2019" 5 "Q1-2020" 6 "Q3-2020" 7 "Q4-2020"
label values t quarter_lab

egen id2=group(id)
egen t2=group(int_date)

drop if telephone==1
drop if missing_emif==1

xtset id2 t

**Municipality size
gen loc_big=0
replace loc_big=1 if pop>200000

**Migration intensity (migrants_rate_q is the migrants rate quantile)
gen migrants_high=(migrants_rate_q=="q3" | migrants_rate_q=="q4" | migrants2010_rate_q=="q3" | migrants2010_rate_q=="q4")

keep if status1=="active"

/*
Main analysis: hours worked
*/

collapse (mean) hrwage whrs covid_exp_w covid_exp_mx [aw=weight], by(mx_mun t)
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
keep if e(sample)==1
save "$output/base.dta", replace
keep mx_mun t covid_exp_w
save "$output/base2.dta", replace

/*
Placebo simulation
*/
clear
program placebo
clear
set obs 7
gen t=_n
gen double rand=runiform()
sort rand
gen new_t=_n
tempfile temp2
save `temp2', replace

use "$output/base.dta", clear
keep whrs mx_mun t covid_exp_mx
merge m:1 t using `temp2'
drop _merge
drop t 
rename new_t t
merge 1:1 mx_mun t using "$output/base2.dta"
reghdfe whrs covid_exp_w covid_exp_mx, absorb(mx_mun t) cluster(mx_mun)
end

simulate _b[covid_exp_w] _se[covid_exp_w], reps(1000) seed(11123): placebo
rename _sim_1 beta
rename _sim_2 stderror
gen tstat=beta/stderror

label var beta "Coefficient"

sum, detail 

count if beta>0.00351
count if t>3.83

grstyle init
grstyle set plain, horizontal compact minor
grstyle set legend 4, nobox klength(small)
grstyle set color hue, n(3) opacity(50)

cd "$output"

gr tw kdensity beta, graphregion(color(white)) xline(0.00351) title("Time-series permutations: Exogeneity", size(small)) range(-0.008 0.008) ///
text(198 0.00351 "{&beta}=0.00351 (p-value=0.014)", place(sw)) ytitle("Density") xtitle("Coefficients")
graph save beta_placebo_years.gph, replace
graph export "$output/figure_3_a.png", replace

gr tw kdensity tstat, graphregion(color(white)) xline(3.83) title("Time-series permutations: Inference", size(small)) range(-3.7 3.7) ///
text(.2 3.83 "t=3.83 (p-value=0.016)", place(sw)) ytitle("Density") xtitle("t-statistics")
graph save t_placebo_years.gph, replace
graph export "$output/figure_3_b.png", replace

graph combine beta_placebo_years.gph t_placebo_years.gph, col(2) graphregion(color(white))
graph export "$output/figure_3.png", replace









